package com.tsingteng.cosfun.ui.mine.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;


/**
 * Created by yangzhikuan .
 */
public abstract class CommonAdapter<T> extends BaseAdapter {

    public Context context;
    protected List<T> data = new ArrayList<T>();
    private T allData;

    private int layoutId;
    public MultiItemTypeSupport<T> mMultiItemSupport;

    public CommonAdapter(Context context, List<T> data, int layoutId) {
        this.context = context;
        if (data != null) {
            this.data.addAll(data);
        }
        this.layoutId = layoutId;
    }


    public CommonAdapter(Context context, List<T> data, int layoutId, MultiItemTypeSupport<T> mMultiItemSupport) {
        this(context, data, layoutId);
        this.mMultiItemSupport = mMultiItemSupport;
    }


    public CommonAdapter(Context context, List<T> data, MultiItemTypeSupport<T> mMultiItemSupport) {
        this.context = context;
        if (data != null) {
            this.data.addAll(data);
        }
        this.mMultiItemSupport = mMultiItemSupport;
    }

    public ViewHolder getAdapterHelper(int position,
                                       View convertView, ViewGroup parent, int count) {
        if (mMultiItemSupport != null) {
            return get(
                    context,
                    convertView,
                    parent,
                    mMultiItemSupport.getLayoutId(position, getItem(position)),
                    position, count);
        } else {
            return get(context, convertView, parent, layoutId, count, position);
        }
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public T getItem(int position) {

        if (data == null && allData != null) {
            return allData;
        }
        if (data.size() > 0 && data.get(position) != null) {
            return data.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

//        ViewHolder holder = ViewHolder.get(context, convertView, parent, layoutId, getCount());
        ViewHolder holder = getAdapterHelper(position, convertView, parent, getCount());
        convert(holder, getItem(position), position);
        return holder.getConvertView();
    }


    ViewHolder get(Context context, View convertView,
                   ViewGroup parent, int layoutId, int count, int position) {
        if (convertView == null) {
            return new ViewHolder(context, parent, layoutId, count, position, false);
        }

        // Retrieve the existing helper and update its position
        ViewHolder helper = (ViewHolder) convertView.getTag();

        if (helper.layoutId != layoutId) {
            return new ViewHolder(context, parent, layoutId, count, position, false);
        }

        helper.position = position;
        return helper;
    }


//    public ViewHolder getViewHolder(){
//        return
//    }

    @Override
    public int getViewTypeCount() {
        if (mMultiItemSupport != null && data.size() >= 1)
            return mMultiItemSupport.getViewTypeCount(data.size());
        return 1;
    }

    @Override
    public int getItemViewType(int position) {

        if (mMultiItemSupport != null)
            return position >= data.size() ? 0 : mMultiItemSupport
                    .getItemViewType(position, getItem(position));

        return position >= data.size() ? 0 : 1;

    }


    /**
     * 更新数据
     *
     * @param items
     */
    public void setData(List<T> items, boolean isRefrsh) {
        if (items == null) {
            return;
        }
        if (isRefrsh) {
            data.clear();
        }
        data.addAll(items);
        notifyDataSetChanged();
    }

    /**
     * 更新数据
     *
     * @param items
     */
    public void setData(T items) {
        if (items == null) {
            return;
        }
        allData = items;
        notifyDataSetChanged();
    }
    public void clearData() {
        data.clear();
    }
    /**
     * 更新数据
     *
     * @param items
     */
    public void removeData(T items) {
        if (items == null) {
            return;
        }
        if (data != null) {
            data.remove(items);
        }
        notifyDataSetChanged();
    }

    public void insert(T object, int index) {
        if (data != null) {
            data.add(index, object);
        }
        notifyDataSetChanged();
    }

    public void setDataAndNotify(List<T> items) {
        if (items == null) {
            return;
        }
        if (data != null) {
            data.clear();
        } else {
            data = new ArrayList<T>();
        }
        data.addAll(items);
        notifyDataSetChanged();
    }

    /**
     * 更新数据
     *
     * @param items
     */
    public void removeDataAndAdd(List<T> items, List<T> newItems) {
        if (items == null) {
            return;
        }
        if (data == null) {
            return;
        }
        if (data.containsAll(items)) {
            data.removeAll(items);
            data.addAll(newItems);
            notifyDataSetChanged();
        }

    }

    /**
     * 更新数据
     *
     * @param items
     */
    public void addData(List<T> items) {
        if (items == null) {
            return;
        }
        if (data != null) {
            data.addAll(items);
        } else {
            data = new ArrayList<T>();
        }
        notifyDataSetChanged();
    }

    public List<T> getData() {
        return data;
    }

    public void clear() {
        if (data != null) {
            data.clear();
        }
        notifyDataSetChanged();
    }

    public abstract void convert(ViewHolder holder, T item, int positon);

    public class ViewHolder {
        private SparseArray<View> views;
        private View convertView;
        public int layoutId;
        public int count;
        public int position;

        public <T extends View> T getView(int viewId) {
            View view = views.get(viewId);
            if (view == null) {
                view = convertView.findViewById(viewId);
                views.put(viewId, view);
            }
            return (T) view;
        }

        private ViewHolder(Context context, ViewGroup parent, int layoutId, int count, int position) {


            this.views = new SparseArray<View>();
            convertView = LayoutInflater.from(context).inflate(layoutId, null);
            this.layoutId = layoutId;
            convertView.setTag(this);
            this.count = count;
            this.position = position;
        }

        private ViewHolder(Context context, ViewGroup parent, int layoutId, int count, int position, boolean isReuse) {


            this.views = new SparseArray<View>();
            convertView = LayoutInflater.from(context).inflate(layoutId, null);
            this.layoutId = layoutId;
            convertView.setTag(this);
            this.count = count;
            this.position = position;
        }

        public View getConvertView() {
            return convertView;
        }


        public ViewHolder setText(int viewId, String text) {
            TextView view = getView(viewId);
            view.setText(text);
            return this;
        }


        public ViewHolder setTextFromHtml(int viewId, String text) {
            TextView view = getView(viewId);
            view.setText(Html.fromHtml(text));
            return this;
        }

        public ViewHolder setTextColor(int viewId, int color) {
            TextView view = getView(viewId);
            view.setTextColor(color);
            return this;
        }

        public ViewHolder setViewVisibility(int viewId, int visibility) {
            View view = getView(viewId);
            view.setVisibility(visibility);
            return this;
        }


        public ViewHolder setViewBackgroundColor(int viewId, int res) {
            View view = getView(viewId);
            view.setBackgroundColor(res);
            return this;
        }

        public ViewHolder setViewImageResource(int viewId, int res) {
            ImageView view = getView(viewId);
            view.setImageResource(res);
            return this;
        }


        public ViewHolder setCommonTextValue(int viewId, String text) {
            TextView view = getView(viewId);
            view.setText(text);
            return this;
        }

        /**
         * 为ImageView设置图片
         *
         * @param viewId
         * @param drawableId
         * @return
         */
        public ViewHolder setImageResource(int viewId, int drawableId) {
            ImageView view = getView(viewId);
            view.setImageResource(drawableId);

            return this;
        }

        public ViewHolder setViewOnClickListener(int viewId, OnClickListener l) {
            ImageView view = getView(viewId);
            view.setOnClickListener(l);

            return this;
        }

        /**
         * 为ImageView设置图片
         *
         * @param viewId
         * @param bm     drawableId
         * @return
         */
        public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
            ImageView view = getView(viewId);
            view.setImageBitmap(bm);
            return this;
        }


    }

}
